from appium import webdriver
from appium.webdriver.webdriver import WebDriver
from appium.webdriver import WebElement
from appium.webdriver.common.touch_action import TouchAction

def touch_action(driver,x,y):
    TouchAction(driver).tap(x=x, y=y).perform()

def swipe(driver: WebDriver,_from,_to,t=None) -> None:
    if isinstance(_from,dict):
        swipe_by_location(driver,_from,_to,t)
    else:
        swipe_by_web_element(driver,_from,_to,t)

def swipe_by_web_element(
        driver: WebDriver,
        web_ele_1: WebElement,
        web_ele_2: WebElement,
        t=None) -> None:
    swipe_by_location(driver,web_ele_1.location,web_ele_2.location,t)

def swipe_by_location(
        driver: WebDriver,
        location_1: dict,
        location_2: dict,
        t=None) -> None:
    driver.swipe(location_1['x'], location_1['y'],
                 location_2['x'], location_2['y'],t)

def get_point(ele:WebElement,rate_x,rate_y):
    x_min = ele.location['x']
    y_min = ele.location['y']
    x_max = x_min + ele.size["width"]
    y_max = y_min + ele.size["height"]
    x = x_min+rate_x*(x_max-x_min)
    y = y_min+rate_y*(y_max-y_min)
    return {'x':x,'y':y}

